package com.aizhixin.lab.docker.service;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.docker.domain.ContainerDomainV2;
import com.aizhixin.lab.docker.domain.FormData;
import com.aizhixin.lab.docker.entity.ContainerToImages;
import com.aizhixin.lab.docker.entity.LastOperation;
import com.aizhixin.lab.docker.repository.ContainerToImagesRepository;
import com.aizhixin.lab.docker.repository.LastOperationRepository;

import lombok.SneakyThrows;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Transactional
public class ContainerToImage implements Runnable {
    private DockerContainerService dockerContainerService;
    private ContainerToImagesRepository containerToImagesRepository;
    private ContainerDomainV2 containerDomainV2;
    private AccountDTO accountDTO;
    private String token;
    private LastOperationRepository lastOperationRepository;
    private Long expire;

    public ContainerToImage(DockerContainerService dockerContainerService, ContainerToImagesRepository containerToImagesRepository, ContainerDomainV2 containerDomainV2, AccountDTO accountDTO, String token, LastOperationRepository lastOperationRepository,Long expire) {
        this.dockerContainerService = dockerContainerService;
        this.containerToImagesRepository = containerToImagesRepository;
        this.containerDomainV2 = containerDomainV2;
        this.accountDTO = accountDTO;
        this.token = token;
        this.lastOperationRepository = lastOperationRepository;
        this.expire=expire;
    }

    @SneakyThrows
    @Override
    @Transactional
    public void run() {


        Thread.sleep((expire-60L)*1000L);
        String save = dockerContainerService.save(containerDomainV2, accountDTO, token);
        if (save!=null) {
            ContainerToImages containerToImages = new ContainerToImages();
            containerToImages.setId(UUID.randomUUID().toString());
            containerToImages.setImagesName(accountDTO.getUserName());
            containerToImages.setImageVersion(save);
            containerToImages.setUserId(accountDTO.getId());
            containerToImages.setCourseId(containerDomainV2.getCourseId());
            containerToImages.setChapterId(containerDomainV2.getChapterId());
            containerToImages.setCreateDate(new Date());

            int i = dockerContainerService.deleteContainerToImage(containerDomainV2.getCourseId(), containerToImages.getChapterId(), accountDTO.getId());
            Integer num = dockerContainerService.getNum(containerDomainV2.getCourseId(), containerDomainV2.getChapterId(), accountDTO.getId());
            containerToImagesRepository.save(containerToImages);
            if (num==0){
                lastOperationRepository.deleteByUserId(accountDTO.getId());
                LastOperation lastOperation = new LastOperation();
                lastOperation.setUserId(accountDTO.getId());
                lastOperation.setImagesVersion(save);
                lastOperation.setImagesName(accountDTO.getUserName());
                lastOperation.setId(UUID.randomUUID().toString());
                lastOperation.setCourseId(containerDomainV2.getCourseId());
                lastOperation.setCreateDate(new Date());
                lastOperationRepository.save(lastOperation);
            }
        }
    }
}
